using System;
using System.Collections.Generic;
using System.Text;
using Mpi.Common.Collections;
using System.ComponentModel;

namespace Xeml.Document
{
    public class IndividualsPool : XemlAnnotatableObject
    {

        private string _ns;
        private string _germPlasm;
        private IndividualsCollection _individuals;
 
        #region Properties
        [Browsable(false)]
        public IndividualsCollection Individuals
        {
            get { return _individuals; }
        }

        /// <summary>
        /// The germplasm identifiere 
        /// </summary>
        [Browsable(false)]
        public string GermPlasm
        {
            get { return _germPlasm; }
            set { _germPlasm = value; }
        }


        /// <summary>
        /// The namepace for the germplasm
        /// </summary>
        [Browsable(false)]
        public string NameSpace
        {
            get { return _ns; }
            set { _ns = value; }
        }

        /// <summary>
        /// The full qualified name. Is a combination of the name space followed by the name and separated by a colon.
        /// </summary>
        /// <exception cref="System.FormatException">Will be throwed if the passed value has a wrong format. Correct formats are like 'MyNameSpace:MyParameter'.</exception>
        [Browsable(true)]
        public string FqName
        {
            get
            {
                return BuildFqName(this._ns, this._germPlasm);
                //if (String.IsNullOrEmpty(this.NameSpace) || "none".Equals(this.NameSpace.ToLower()))
                //{
                //    return this.name;
                //}
                //else
                //{
                //    return String.Format("{0}:{1}", this.NameSpace, this.Name);
                //}
            }
            set
            {
                string[] temp = value.Split(':');
                if (temp.Length != 2)
                {
                    throw new FormatException("The value has the wrong format.");
                }
                else
                {
                    string oldVal = this.FqName;
                    this._ns = temp[0];
                    this._germPlasm = temp[1];
                    if (oldVal != this.FqName)
                    {
                        OnChanged();
                    }
                }
            }
        } 
        #endregion

        #region Methods

        private string BuildFqName(string nameSpace, string name)
        {
            if (String.IsNullOrEmpty(nameSpace) || "none".Equals(nameSpace.ToLower()))
            {
                return name;
            }
            else
            {
                return String.Format("{0}:{1}", nameSpace, name);
            }
        } 
        #endregion

        #region ctors
        public IndividualsPool()
        {
            this._individuals = new IndividualsCollection(this);
        } 
        #endregion



        public event EventHandler Changed;
        private void OnChanged()
        {
            if (this.Changed != null)
                this.Changed(this, new EventArgs());
        }


        private IndividualsPoolCollection _collection;
        [Browsable(false)]
        public IndividualsPoolCollection Collection
        {
            get
            {
                return this._collection;
            }
            set
            {
                if (value != _collection)
                {
                    _collection = value;
                }

            }
        }

      
    }
}
